﻿using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Scope;
using IBatisNet.DataMapper.MappedStatements;
using System;
using IBatisNet.Common.Transaction;

namespace Shop.Data.IBatis
{
    public interface ITransaction : IDisposable
    {
        void BeginTransaction();

        void BeginTransaction(System.Data.IsolationLevel isolationLevel);

        void Complete();
    }

    /// <summary>
    /// 数据库事务类。只针对写库启动事务。
    /// </summary>
    //public class Transaction<T> : ITransaction where T : IBatisDaoBase, new()
    //{
    //    protected IBatisDaoBase daoBase = null;
    //    public Transaction()
    //    {
    //        this.daoBase = new T();
    //    }
    //    private ISqlMapSession iSession = null;

    //    /// <summary>
    //    /// 创建事务对象。
    //    /// </summary>
    //    /// <returns>事务实例</returns>
    //    /// <example>
    //    /// Transaction tran = new Transaction();
    //    /// using (ISqlMapSession iSession = tran.BeginTransaction())
    //    /// {
    //    ///     //do some thing
    //    ///     iSession.Complete();
    //    /// } 
    //    /// </example>
    //    public ISqlMapSession BeginTransaction()
    //    {
    //        this.iSession = this.daoBase.Writer.BeginTransaction();
    //        return this.iSession;
    //    }

    //    /// <summary>
    //    /// 创建事务对象。
    //    /// </summary>
    //    /// <param name="isolationLevel">事务级别</param>
    //    /// <returns>事务实例</returns>
    //    /// <example>
    //    /// Transaction tran = new Transaction();
    //    /// using (ISqlMapSession iSession = tran.BeginTransaction(IsolationLevel))
    //    /// {
    //    ///     //do some thing
    //    ///     session.Complete();
    //    /// } 
    //    /// </example>
    //    public ISqlMapSession BeginTransaction(System.Data.IsolationLevel isolationLevel)
    //    {
    //        this.iSession = this.daoBase.Writer.BeginTransaction(isolationLevel);
    //        return this.iSession;
    //    }

    //    public void Complete()
    //    {
    //        this.iSession.Complete();
    //    }
    //}

    /// <summary>
    /// 默认是写库，写库配置文件名默认为SqlmapWriter.config。
    /// 如果只有操作一个数据库，可以使用这个简单的事务类。
    /// 这个类的关键是默认从SqlmapWriter.config读取配置。
    /// </summary>
    public class SimpleTransaction : DaoBase, ITransaction
    {
        private ISqlMapSession iSession = null;

        /// <summary>
        /// 创建事务对象。
        /// </summary>
        /// <returns>事务实例</returns>
        /// <example>
        /// Transaction tran = new Transaction();
        /// using (ISqlMapSession iSession = tran.BeginTransaction())
        /// {
        ///     //do some thing
        ///     iSession.Complete();
        /// } 
        /// </example>
        public void BeginTransaction()
        {
            this.iSession = this.Writer.BeginTransaction();
        }

        /// <summary>
        /// 创建事务对象。
        /// </summary>
        /// <param name="isolationLevel">事务级别</param>
        /// <returns>事务实例</returns>
        /// <example>
        /// Transaction tran = new Transaction();
        /// using (ISqlMapSession iSession = tran.BeginTransaction(IsolationLevel))
        /// {
        ///     //do some thing
        ///     session.Complete();
        /// } 
        /// </example>
        public virtual void BeginTransaction(System.Data.IsolationLevel isolationLevel)
        {
            this.iSession = this.Writer.BeginTransaction(isolationLevel);
        }

        public virtual void Complete()
        {
            this.iSession.Complete();
        }
        
        public virtual void Dispose()
        {
           this.iSession.Dispose();
        }
    }
}
